package com.zxj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seventh.common.pojo.R;
import com.zxj.mapper.CarBrandMapper;
import com.zxj.pojo.entity.TbBrand;
import com.zxj.pojo.entity.TbCar;
import com.zxj.pojo.entity.TbCarTypeConfigDetails;
import com.zxj.pojo.vo.BrandVo;
import com.zxj.service.CarBrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: clms-seventh
 * @description:
 * @author: 张修杰
 * @create: 2021-07-24 17:06
 **/
@Service
public class CarBrandServiceImpl implements CarBrandService {
    @Resource
    CarBrandMapper carBrandMapper;

    @Override
    public R selectAllCarBrand() {
        List<TbBrand> tbBrands =carBrandMapper.selectList(null);
        return R.ok().put("AlltbBrands",tbBrands);
    }

    @Override
    public R selectCarBrand(TbBrand tbBrand) {
        QueryWrapper<TbBrand> wrapper=new QueryWrapper<>();
        //System.out.println(tbBrand);
        if (StringUtils.isNotEmpty(tbBrand.getName())){
            wrapper.eq("name",tbBrand.getName());
        }
        if (StringUtils.isNotEmpty(tbBrand.getId())){

            wrapper.ne("id",tbBrand.getId());
        }
        if (StringUtils.isNotEmpty(tbBrand.getName())){
            TbBrand tbBrand1=carBrandMapper.selectOne(wrapper);
            if (tbBrand1!=null){
                return R.error().message("已经存在");
            }else {
                return R.ok().message("成功");
            }
        }else {
            return R.error().message("不能为空");
        }
    }

    @Override
    public R findAllCarBrandPage(Integer currentPage, Integer pageSize, BrandVo brandVo) {
        QueryWrapper<TbBrand> wrapper=new QueryWrapper<>();

        if (StringUtils.isNotEmpty(brandVo.getName())){
            wrapper.like("name",brandVo.getName());
        }
        Page<TbBrand> page=new Page<>(currentPage,pageSize);
//        IPage<TbCar> page1=carConfigMapper.SaveCarList(page,wrapper);
        carBrandMapper.selectPage(page,wrapper);
        return R.ok().put("records",page.getRecords()).put("total",page.getTotal());

    }

    @Override
    public R updateCarBrandById(TbBrand tbBrand) {
        int i= carBrandMapper.updateById(tbBrand);
        return (i>0)? R.ok():R.error();
    }

    @Override
    public R selectCarBrandById(String id) {
        TbBrand tbBrand=carBrandMapper.selectById(id);
        return R.ok().put("tbBrand",tbBrand);
    }

    @Override
    public R insertCarBrand(TbBrand tbBrand) {
        int i=carBrandMapper.insert(tbBrand);
        return (i>0)? R.ok():R.error();
    }

    @Override
    public R deleteCarBrand(String id) {
        int i=carBrandMapper.deleteById(id);
        return (i>0)?R.ok():R.error();
    }
}
